package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.mapper.SysRoleMapper;
import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author MikeCoder
 * @create 2023-12-0420:59
 * @description:
 * @verson:
 */
@RestController
@RequestMapping(value = "/admin/system/sysRole")
@Tag(name = "角色管理接口",description = "角色管理相关的接口方法")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping ("/findByPage/{pageNum}/{pageSize}")
    @Operation(summary = "按分页条件，展示数据",description = "按，页数，页码，和搜索关键字，查询记录")
    public Result<PageInfo<SysRole>> findByPage(SysRoleDto sysRoleDto,
                                                @PathVariable Integer pageNum,
                                                @PathVariable Integer pageSize
    ){
        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(pageNum, pageSize, sysRoleDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/saveSysRole")
    @Operation(summary = "添加角色信息")
    public Result saveSysRole(@RequestBody SysRole sysRole){
        sysRoleService.add(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/getSysRoleById/{id}")
    @Operation(summary = "根据id查询角色信息")
    public Result<SysRole> getSysRoleById(@PathVariable Integer id){
        System.out.println(id);
        SysRole sysRoleById = sysRoleService.getSysRoleById(id);
        System.out.println(sysRoleById);
        return Result.build(sysRoleById,ResultCodeEnum.SUCCESS);
    }

    @PutMapping("/change")
    @Operation(summary = "修改角色信息")
    public Result change(@RequestBody SysRole sysRole){
        sysRoleService.change(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/remove/{id}")
    @Operation(summary = "删除角色信息")
    public Result remove(@PathVariable Integer id){
        sysRoleService.remove(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "用户分配角色信息")
    @GetMapping("/userRoleInfo/{id}")
    public Result userRoleInfo(@PathVariable Integer id){
        Map<String,Object> map =  sysRoleService.userRoleInfo(id);
        return Result.build(map,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询角色分配菜单信息")
    @GetMapping("/findRoleMenuId/{roleId}")
    public Result findRoleMenuId(@PathVariable Long roleId){
        List<Long> idList =  sysRoleService.findRoleMenuId(roleId);
        return Result.build(idList,ResultCodeEnum.SUCCESS);
    }
}
